(function (exports) {
    var $ = null;
    var EmailShowContent = cc.Layer.extend({
        contentData: [],
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function (data) {
            this._super();
            var that = this;

            var scene = loadNodeCCS(res.EmailContent_json, this);
            $ = create$(this.getChildByName('Layer'));
            this._$ = $;
            that._data = data;

            that.initContentUI(data);

            TouchUtil.setOnclickListener(this._$('pannelBg.btn_close'), function (node) {
                that.removeFromParent(true);
            });
            TouchUtil.setOnclickListener(this._$('pannelBg.btn_lingqu'), function (node) {
                var data2 = {
                    userid: gameData.uid + "",
                    gameid: app.area + "",
                    mailid: [data.mailid],
                };
                network.sendProto(1029, data2);
                that.removeFromParent(true);
            });
            if (data.get == 2 || data.attachments.length == 0) { //已领取
                this._$('pannelBg.btn_lingqu').setVisible(false);
            }
        },
        initContentUI: function (data) {
            var that = this;
            this._$('pannelBg.txt_title').setString(data.subject);
            this._$('pannelBg.txt_content').setString(data.content);

            var attachments = data.attachments;
            this._$('pannelBg.txt_fujian').setVisible(attachments.length > 0);

            that.contentData = attachments;
            that.initContentData();
        },
        initContentData: function () {
            var that = this;
            if (that.tableview) {
                tableViewRefresh(that.tableview); //刷新一遍 用于查询
            } else {
                var tableviewLayer = that._$('pannelBg.tableview');
                var tableview = new cc.TableView(that, cc.size(tableviewLayer.getContentSize().width, tableviewLayer.getContentSize().height));
                tableview.setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL);
                tableview.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
                tableview.x = 0;
                tableview.y = 0;
                tableview.setDelegate(that);
                tableview.setBounceable(true);
                tableviewLayer.addChild(tableview);
                that.tableview = tableview;
            }
        },
        //点击cell
        tableCellTouched: function (table, cell) {
        },
        tableCellSizeForIndex: function (table, idx) {
            return cc.size(120, 100);
        },
        tableCellAtIndex: function (table, idx) {
            var that = this;
            var cell = table.dequeueCell();
            if (cell == null) {
                cell = new cc.TableViewCell();
                var row0 = duplicateNode(that._$('pannelBg.imgItem0'));
                row0.setName('cellrow');
                row0.setPosition(0, 0);
                row0.setAnchorPoint(0, 0);
                cell.addChild(row0);
            }
            var Layer = cell.getChildByName('cellrow');
            var contentData = that.contentData[idx];
            if (contentData) {
                var moneyMap = {
                    coin: 'item_coin',
                    roomcard: 'item_card',
                    diamond: 'item_diamond'
                };
                var itemid = contentData.itemid;
                if (moneyMap[itemid]) { //货币从本地获取
                    that._$('item', Layer).removeAllChildren();
                    var sp = new cc.Sprite(that._$(moneyMap[itemid]).getTexture());
                    that._$('item', Layer).addChild(sp, 10);
                } else { //url 获取
                    var itemInfo = ItemData.getItemInfoByCode(itemid);
                    var imageUrl = itemInfo.ImgSml || itemInfo.ImgBig;
                    downLoadImage(imageUrl, function (texture) {
                        that._$('item', Layer).removeAllChildren();
                        var sp = new cc.Sprite(texture);
                        sp.setScale(0.5);
                        that._$('item', Layer).addChild(sp, 10);
                    });
                }
                that._$('num', Layer).setString(equalNum(contentData.itemvalue));
                that._$('hadLing', Layer).setVisible(that._data.get == 2);
            }
            return cell;
        },
        numberOfCellsInTableView: function (table) {
            return this.contentData.length;
        },


    });
    exports.EmailShowContent = EmailShowContent;

})(window);